package com.wjw.java.io.inputStream_;

import com.wjw.java.io.Students;

import java.io.*;

/**
 * 对象输入流：反序列化
 */
public class ObjectInputStream_ {

    public static void main(String[] args) {
        //读取(反序列化)的顺序,需要和写入的顺序保持一致
        String filePath = "f:\\IO\\data.dat";
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(filePath));
            int intVal = ois.readInt();
            System.out.println("intVal:"+intVal);

            boolean boolVal = ois.readBoolean();
            System.out.println("boolVal:"+boolVal);

            char charVal = ois.readChar();
            System.out.println("charVal:"+charVal);

            double doubleVal = ois.readDouble();
            System.out.println("doubleVal:"+doubleVal);

            String strVal = ois.readUTF();
            System.out.println("strVal:"+strVal);

            Students student = (Students) ois.readObject();
            System.out.println("student:"+student);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    //关闭流的外层即可,底层会关闭FileInputStream
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

